<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/22 0022
 * Time: 14:58
 */

namespace app\finance\controller;


use think\Db;

class ExpenseController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    /**
     *创建用户
     */
    public function addExpense()
    {
        echo $this->fetch();
    }

    /**
     * 保存创建用户数据
     * @return mixed
     */
    public function ajaxAddExpense()
    {
        if(!empty($_POST)){
            $data = $this->request->post('data');
            $isStartTimeExist = Db::table('expense')
                ->whereBetweenTime('start_time', $data['start_time'])
                ->where('create_user_id', '=', session('user_id'))
                ->count();
            if($isStartTimeExist > 0)  return $this->ajaxFail(-1, '已经存在起止日期相同的报销单，不能重复创建');
            unset($data['file']);
            $data['create_user_id'] = session('user_id');
            $addResult = Db::table('expense')->data($data)->insert();
            if($addResult) return $this->ajaxSuccess();
        }
        return $this->ajaxFail();
    }

    /**
     * 上传文件
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function uploadImg()
    {
        $file = $this->request->file('file');

        $fileInfo = $file->validate(['ext'=>'jpg,png,gif'])->move('./expense_img/',true,false);
        if($fileInfo){
            $data = array();
            $data['file_name'] = $_FILES['file']['name'];
            $data['file_path'] = $fileInfo->getSaveName();
            $data['create_time'] = date('Y-m-d H:i:s');
            $insertRes = Db::table('file_path')->data($data)->insert();
            if(!$insertRes)  return $this->ajaxFail();
          return $this->ajaxSuccess(1,'',array('file_id'=> Db::getLastInsID()));
        }else{
            return $this->ajaxFail(-1, $file->getError());
        }

    }
    /**
     *编辑用户
     */
    public function editExpense()
    {
        $id = $this->request->get('id');
        if(!is_numeric($id))return $this->ajaxFail(0,'参数错误');
        $data = Db::table('expense')
            ->alias('a')
            ->field('a.*, b.file_path as file_id')
            ->leftJoin('file_path b', 'a.file_id = b.id')
            ->where('a.id', '=', $id)->find();

        $data['fill_time'] =date('Y-m-d',strtotime( $data['fill_time']));
        $data['start_time'] =date('Y-m-d', strtotime($data['start_time']));
        $data['end_time'] =date('Y-m-d', strtotime($data['end_time']));
        $data['file_id'] = 'http://' . $_SERVER['HTTP_HOST']. '/expense_img/' .$data['file_id'];

        if(empty($data)) return '信息不存在';
        $this->assign('data', $data);
        echo  $this->fetch();
    }

    /**
     * 查看
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function showExpense()
    {
        $id = $this->request->get('id');
        if(!is_numeric($id))return $this->ajaxFail(0,'参数错误');
        $data = Db::table('expense')
            ->alias('a')
            ->field('a.*, b.file_path as file_id')
            ->leftJoin('file_path b', 'a.file_id = b.id')
            ->where('a.id', '=', $id)->find();
        if(empty($data)) return '信息不存在';
        if(session('user_name') == 'admin'){
//            Db::table('expense')->where('id', '=', $data['id'])->data(array('status' => 2))->update();
        }
        $data['fill_time'] =date('Y年m月d日',strtotime( $data['fill_time']));
        $data['start_time'] =date('Y-m-d', strtotime($data['start_time']));
        $data['end_time'] =date('Y-m-d', strtotime($data['end_time']));
        $data['file_id'] = 'http://' . $_SERVER['HTTP_HOST']. '/expense_img/' .$data['file_id'];
        $data['sum'] = $data['eat_subsidy_money'] + $data['stay_money'] + $data['traffic_money'] + $data['sleeper_money'] + $data['city_traffic_money'] + $data['road_bridge_money'];
        $data['sum_chinese'] = getChineseNumber($data['sum']);
        $this->assign('data', $data);
//        return $this->fetch();
        echo  $this->fetch();
    }

    public function getNoReadyCount()
    {
        $count = Db::table('expense')->where('status', '=', 1)->count();
        return $this->ajaxSuccess(1,'',['count' => $count]);
    }

    /**
     * 获取数据 old
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function showExpenseData()
    {
        $id = $this->request->get('id');
        if(!is_numeric($id))return $this->ajaxFail(0,'参数错误');
        $data = Db::table('expense')
            ->alias('a')
            ->field('a.*, b.file_path as file_id')
            ->leftJoin('file_path b', 'a.file_id = b.id')
            ->where('a.id', '=', $id)->find();
        if(empty($data)) return '信息不存在';
        $data['fill_time'] =date('Y-m-d',strtotime( $data['fill_time']));
        $data['start_time'] =date('Y-m-d', strtotime($data['start_time']));
        $data['end_time'] =date('Y-m-d', strtotime($data['end_time']));
        $data['file_id'] = 'http://' . $_SERVER['HTTP_HOST']. '/expense_img/' .$data['file_id'];
        $data['sum'] = $data['eat_subsidy_money'] + $data['stay_money'] + $data['traffic_money'] + $data['sleeper_money'] + $data['city_traffic_money'] + $data['road_bridge_money'];
        return $data;
    }
    /**
     * 保存创建用户数据
     * @return mixed
     */
    public function ajaxEditExpense()
    {
        if(!empty($_POST)){
            $data = $this->request->post('data');
            unset($data['file']);
            $Result = Db::table('expense')->data($data)->update();
            if($Result or $Result == 0) return $this->ajaxSuccess();
        }
        return $this->ajaxFail();
    }

    /**
     * @return array
     * user search
     */
    public function ajaxExpenseChaXun()
    {
        $map = array();
//        if(session('user_name') != 'admin') return $data = array('code' => 0, 'msg' => '', 'count' => 0, 'data' => array());
//        if(!empty($_GET) and (isset($_GET['name']) or isset($_GET['card_num']))){
//            if(!empty($_GET['name'])) $map[] = ['user_name','=',$_GET['name']];
//            if(!empty($_GET['card_num'])) $map[] = array('card_num', '=',$_GET['card_num']);
//
//            $array = Db::table('user_info')->where($map)->order('id','desc')->select();
//            $count = Db::table('user_info')->where($map)->count();
//            $data = array('code' => 0, 'msg' => '', 'count' => $count, 'data' => $array);
//            return $data;
//        }
        $page = empty($_GET['page']) ? 1 :$_GET['page'];
        $limit = empty($_GET['limit']) ? 15 :$_GET['limit'];
        if(session('user_name') != 'admin') $map[]  = ['create_user_id', '=', session('user_id')];
        $array = Db::table('expense')
            ->field('a.*, b.file_path as file_id')
            ->alias('a')
            ->leftJoin('file_path b', 'a.file_id = b.id')
            ->where($map)
            ->limit($page - 1, $limit)
            ->order('id','desc')
            ->select();

        foreach ($array as $key => $item) {
            $array[$key]['fill_time'] =date('Y-m-d',strtotime( $item['fill_time']));
            $array[$key]['start_time'] =date('Y-m-d', strtotime($item['start_time']));
            $array[$key]['end_time'] =date('Y-m-d', strtotime($item['end_time']));
            $array[$key]['file_id'] = 'http://' . $_SERVER['HTTP_HOST']. '/expense_img/' .$item['file_id'];
        }
        $count = Db::table('expense')->count();
        $data = array('code' => 0, 'msg' => '', 'count' => $count, 'data' => $array);
        return $data;
    }

    /**
     *删除
     */
    public function delete()
    {
        if(!empty($_POST) and isset($_POST['checkIdS'])){
            $checkIdArray = explode(',', trim($_POST['checkIdS'], ','));
            $delResult = Db::table('expense')->delete($checkIdArray);
            if($delResult) return $this->ajaxSuccess();
        }
        return $this->ajaxFail();
    }
}